package com.xiaochong.tir.manager.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaochong.tir.common.data.mapper.ExchangeTransactionpairMiddleMapper;
import com.xiaochong.tir.common.data.mapper.TransactionPairMapper;
import com.xiaochong.tir.common.data.po.ExchangeTransactionpairMiddle;
import com.xiaochong.tir.common.data.po.TransactionPair;
import com.xiaochong.tir.manager.config.druid.Operation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2018/5/30.
 */
@Service
public class TransactionPairService {

    @Autowired
    private TransactionPairMapper transactionPairMapper;
    @Autowired
    private ExchangeTransactionpairMiddleMapper exchangeTransactionpairMiddleDao;

    public PageInfo<TransactionPair> selectByentityList(TransactionPair transactionPair, Integer pageSize, Integer pageIndex) {
        PageHelper.startPage(pageIndex,pageSize);
        List<TransactionPair> list = this.transactionPairMapper.selectByentityList(transactionPair);
        PageInfo<TransactionPair> info = new PageInfo<TransactionPair>(list);
        return info;
    }

    public void selectByTraList(TransactionPair transactionPair) {
        List<TransactionPair> list = this.transactionPairMapper.selectByentityList(transactionPair);
        if(null != list){
            for (int i = 0; i < list.size(); i++) {
                TransactionPair tra = list.get(i);
                if(StringUtils.isNotBlank(tra.getEnName()) && tra.getEnName().contains("/")){
                    String [] arr = tra.getEnName().split("/");
                    tra.setTransactionName(arr[0]);
                    tra.setEnName(arr[1]);
                    tra.setUpdateTime(new Date());
                    transactionPairMapper.updateByPrimaryKey(tra);
                }
            }
        }
    }

    public PageInfo<TransactionPair> selectByentityLists(TransactionPair transactionPair, Integer pageSize, Integer pageIndex) {
        PageHelper.startPage(pageIndex,pageSize);
        List<TransactionPair> list = this.transactionPairMapper.selectDistenctList(transactionPair);
        PageInfo<TransactionPair> info = new PageInfo<TransactionPair>(list);
        return info;
    }

    public TransactionPair selectById(Integer id) {
        return this.transactionPairMapper.selectBytraId(id);
    }
    @Operation
    @Transactional
    public int insertTransaction(TransactionPair transactionPair) {
        return this.transactionPairMapper.insertTransaction(transactionPair);
    }

    public TransactionPair selectByPrimaryKey(Integer id) {
        return this.transactionPairMapper.selectByPrimaryKey(id);
    }

    @Operation
    @Transactional
    public int updateByPrimaryKey(TransactionPair transactionPair, TransactionPair old) {
        return this.transactionPairMapper.updateByPrimaryKey(transactionPair);
    }

    public TransactionPair selectByEntity(TransactionPair tra) {
        return this.transactionPairMapper.selectByEntity(tra);
    }

    public void deleteByPrimaryKey(Integer id) {
        this.transactionPairMapper.deleteByPrimaryKey(id);
    }

    @Operation
    @Transactional
    public void delTransaction(TransactionPair pair) {
       this.transactionPairMapper.deleteById(pair);
    }
}
